package org.yylnb.openplatform.payment.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @author RainLin
 * @date 2020/7/28 - 20:34
 */
@Controller
public class ImageController {
    @RequestMapping("/image/{id}")
    public void getImage(@PathVariable String id, HttpServletRequest request, HttpServletResponse response) {
        BufferedImage image = (BufferedImage) request.getServletContext().getAttribute(id);
        try {
            if (image != null) {
                ImageIO.write(image, "JPEG", response.getOutputStream());
                request.getServletContext().removeAttribute(id);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

